package cn.xiongxf.mq.consumer;

import cn.xiongxf.constant.MQConstant;
import cn.xiongxf.domain.MediaFile;
import cn.xiongxf.result.JsonResult;
import cn.xiongxf.service.IMediaFileService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@RocketMQMessageListener(
        topic = MQConstant.TOPIC_MEDIA_FILE,
        selectorExpression = MQConstant.TAGS_MEDIA_FILE,
        consumerGroup = "service-media-consumer"
        //messageModel = MessageModel.CLUSTERING  //消费模式，集群
)

@Component
@Slf4j
public class MediaFileConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService ;

    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        if (body == null || body.length == 0) return;
        String json = new String(body, StandardCharsets.UTF_8);
        MediaFile mediaFile = JSON.parseObject(json, MediaFile.class);
        JsonResult jsonResult = mediaFileService.handleFile2m3u8(mediaFile);
        log.info("推流结果： {} ",jsonResult);
    }
}
